package com.laolang.shop.modules.auth.handler;

import cn.hutool.core.util.StrUtil;
import com.laolang.shop.common.core.consts.ModuleNameConst;
import com.laolang.shop.common.core.domain.SimpleAjax;
import com.laolang.shop.common.core.util.LogUtil;
import com.laolang.shop.common.core.util.PrintJsonWriter;
import com.laolang.shop.modules.auth.util.SecurityUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/1 20:46
 */
@Slf4j
@AllArgsConstructor
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        LogUtil.info(log, ModuleNameConst.AUTH, StrUtil.format("用户: {} 退出", SecurityUtil.getUserId()));
        PrintJsonWriter.printJson(httpServletResponse, SimpleAjax.ok(true));
    }
}
